import React from 'react';
import { Table, Tag, Tooltip, Card, Typography, Space } from 'antd';
import { ClockCircleOutlined, EnvironmentOutlined, UserOutlined, BookOutlined } from '@ant-design/icons';
import { Course } from '../types/schedule';

const { Text } = Typography;

interface ScheduleTableProps {
  courses: Course[];
  loading?: boolean;
  onEdit?: (course: Course) => void;
  onDelete?: (courseId: string) => void;
}

const ScheduleTable: React.FC<ScheduleTableProps> = ({ 
  courses, 
  loading = false, 
  onEdit, 
  onDelete 
}) => {
  // 时间节次映射
  const timeSlots = [
    '08:00-08:45', '08:55-09:40', '10:00-10:45', '10:55-11:40',
    '14:00-14:45', '14:55-15:40', '16:00-16:45', '16:55-17:40',
    '19:00-19:45', '19:55-20:40', '20:50-21:35', '21:45-22:30'
  ];

  // 星期映射
  const weekDays = ['', '周一', '周二', '周三', '周四', '周五', '周六', '周日'];

  // 课程类型颜色映射
  const getCourseTypeColor = (type: string) => {
    switch (type) {
      case 'required': return 'red';
      case 'elective': return 'blue';
      case 'practice': return 'green';
      default: return 'default';
    }
  };

  // 课程类型文本映射
  const getCourseTypeText = (type: string) => {
    switch (type) {
      case 'required': return '必修';
      case 'elective': return '选修';
      case 'practice': return '实践';
      default: return '未知';
    }
  };

  // 格式化周次显示
  const formatWeeks = (weeks: number[]) => {
    if (weeks.length === 0) return '无';
    
    // 排序
    const sortedWeeks = [...weeks].sort((a, b) => a - b);
    
    // 合并连续周次
    const ranges: string[] = [];
    let start = sortedWeeks[0];
    let end = start;
    
    for (let i = 1; i < sortedWeeks.length; i++) {
      if (sortedWeeks[i] === end + 1) {
        end = sortedWeeks[i];
      } else {
        ranges.push(start === end ? `${start}` : `${start}-${end}`);
        start = sortedWeeks[i];
        end = start;
      }
    }
    ranges.push(start === end ? `${start}` : `${start}-${end}`);
    
    return ranges.join(', ');
  };

  const columns = [
    {
      title: '课程名称',
      dataIndex: 'name',
      key: 'name',
      width: 200,
      render: (text: string, record: Course) => (
        <div>
          <Text strong>{text}</Text>
          <div style={{ marginTop: '4px' }}>
            <Tag color={getCourseTypeColor(record.type)}>
              {getCourseTypeText(record.type)}
            </Tag>
            <Tag color="orange">
              {record.credits}学分
            </Tag>
          </div>
        </div>
      ),
    },
    {
      title: '教师',
      dataIndex: 'teacher',
      key: 'teacher',
      width: 120,
      render: (text: string) => (
        <Space>
          <UserOutlined />
          <Text>{text}</Text>
        </Space>
      ),
    },
    {
      title: '教室',
      dataIndex: 'classroom',
      key: 'classroom',
      width: 120,
      render: (text: string) => (
        <Space>
          <EnvironmentOutlined />
          <Text>{text}</Text>
        </Space>
      ),
    },
    {
      title: '时间',
      key: 'time',
      width: 150,
      render: (record: Course) => (
        <div>
          <div>
            <ClockCircleOutlined style={{ marginRight: '4px' }} />
            <Text>{weekDays[record.dayOfWeek]}</Text>
          </div>
          <div style={{ marginTop: '2px' }}>
            <Text type="secondary" style={{ fontSize: '12px' }}>
              第{record.timeSlot}节
              {record.duration > 1 && `-${record.timeSlot + record.duration - 1}节`}
            </Text>
          </div>
          <div style={{ marginTop: '2px' }}>
            <Text type="secondary" style={{ fontSize: '12px' }}>
              {timeSlots[record.timeSlot - 1]}
              {record.duration > 1 && ` ~ ${timeSlots[record.timeSlot + record.duration - 2]}`}
            </Text>
          </div>
        </div>
      ),
    },
    {
      title: '上课周次',
      dataIndex: 'weeks',
      key: 'weeks',
      width: 200,
      render: (weeks: number[]) => (
        <Tooltip title={`第${formatWeeks(weeks)}周`}>
          <Text>{formatWeeks(weeks)}</Text>
        </Tooltip>
      ),
    },
    {
      title: '操作',
      key: 'action',
      width: 100,
      render: (record: Course) => (
        <Space>
          {onEdit && (
            <Tooltip title="编辑">
              <BookOutlined 
                style={{ cursor: 'pointer', color: '#1890ff' }}
                onClick={() => onEdit(record)}
              />
            </Tooltip>
          )}
        </Space>
      ),
    },
  ];

  return (
    <Card>
      <Table
        columns={columns}
        dataSource={courses}
        rowKey="id"
        loading={loading}
        pagination={false}
        scroll={{ x: 800 }}
        size="small"
        bordered
      />
    </Card>
  );
};

export default ScheduleTable;
